<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>代理实现双向数据绑定</title>
</head>
<body>
    <input type="text" v-model="content">
    <h4 v-bind="content">默认字符串</h4>
    <br>
    <input type="text" v-model="title">
    <input type="text" v-model="title">
    <h4 v-bind="title">默认字符串</h4>
    <script>
        'use strict'
        function bindDate() {
            let proxy = new Proxy({},{
                set(obj,property,value){
                    document.querySelectorAll(`[v-model="${property}"]`).forEach(item => {
                        item.value = value;
                    });
                    document.querySelectorAll(`[v-bind="${property}"]`).forEach(item => {
                        item.innerHTML = value;
                    });
                },
                get(obj,property){}
            });
            this.init = function () {
                const els = document.querySelectorAll('[v-model]');
                els.forEach(item => {
                    item.addEventListener('keyup',function () {
                        proxy[this.getAttribute("v-model")] = this.value;
                    })
                })
            }
        }
        new bindDate().init();
    </script>
</body>
</html>